home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.01 Jan 91 / Object Design / line feed removal / CmyDataFile.c next >
Encoding:
C/C++ Source or Header  |  1989-11-26  |  3.4 KB  |  146 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3. CmyDataFile.c
  4.  
  5.     this file adds a method to the standard definition.  It uses some code 
  6.     borrowed from another project to do it.
  7. */
  8.  
  9. #include "MessageDefines.h"
  10. #include "CmyDataFile.h"
  11.  
  12. /* needed for the code module picked up from another project */
  13. typedef short WORD ;
  14. typedef  char CHAR;
  15.  
  16. long ReadaLine(WORD refNum,CHAR *tempString,WORD  maxChars) ;
  17. #define NIL 0L
  18. static 
  19. union
  20.     {
  21.     HParamBlockRec dfParameterBlock; /* our parameter block for private use */
  22.     CInfoPBRec dirPBBlock;
  23.     }p;
  24.  
  25. /*:...................................................................................*/
  26. long ReadaLine(WORD refNum,CHAR *tempString,WORD  maxChars) 
  27. /* WORD refNum;    /* Mac file reference number*/
  28. /*CHAR *tempString;    /* where to store the data */
  29. /* WORD maxChars;  /* maximum number of characters to read for this line */
  30. /*
  31.     Name: Wade Maxfield
  32.     Date: Feb 21, 1989
  33.     Notes:
  34.     Modification History:
  35.     11/25/89 -- here we mix objects and standard c code.
  36. */
  37. {
  38. WORD error;
  39. WORD stringCount;
  40.  
  41.  
  42. /*
  43. PBGetFPos (paramBlock: ParmBlkPtr; async: BOOLEAN) : OSErr;
  44.  
  45. Trap macro  _GetFPos
  46.  
  47. Parameter block
  48.     —>  12  ioCompletion    pointer
  49.     <—  16  ioResult        word
  50.     —>  24  ioRefNum        word
  51.     <—  36  ioReqCount      long word
  52.     <—  40  ioActCount      long word
  53.     <—  44  ioPosMode       word
  54.     <—  46  ioPosOffset     long word
  55. */
  56.         p.dfParameterBlock.fileParam.ioCompletion = NIL;
  57.         p.dfParameterBlock.ioParam.ioRefNum = refNum;
  58.         error = PBGetFPos (&p.dfParameterBlock,FALSE ) ;/* false = synchronously */
  59.         if (error != noErr)
  60.             return(error);
  61.             
  62. /*PBRead (paramBlock: ParmBlkPtr; async: BOOLEAN) : OSErr;
  63.    Trap macro  _Read
  64.     Parameter block
  65.         --> 12  ioCompletion    pointer
  66.         <-- 16  ioResult    word
  67.         --> 24  ioRefNum    word
  68.         --> 32  ioBuffer    pointer
  69.         --> 36  ioReqCount  long word
  70.         <-- 40  ioActCount  long word
  71.         --> 44  ioPosMode   word
  72.         <-> 46  ioPosOffset long word
  73. */
  74.         p.dfParameterBlock.ioParam.ioBuffer = tempString;
  75.         p.dfParameterBlock.ioParam.ioReqCount =maxChars ;
  76.         /* 0d is cr, $80 is newline mode */
  77.         p.dfParameterBlock.ioParam.ioPosMode = fsAtMark+0x0D80;/* from present counter */
  78.         
  79.         error = PBRead (&p.dfParameterBlock,FALSE ) ;/* false = synchronously */
  80.  
  81.     if (p.dfParameterBlock.ioParam.ioActCount)
  82.         stringCount =p.dfParameterBlock.ioParam.ioActCount-1;
  83.     else
  84.         stringCount = 0;
  85.         
  86.         
  87.         /* all characters exhausted */
  88.         if ( (error == eofErr && stringCount == 0 ) || 
  89.                 (error != noErr && error != eofErr) )
  90.             return(error);
  91.         
  92.     if ( tempString[stringCount] == '\r')
  93.         tempString[++stringCount]=0;  /* delimit string */
  94.     else
  95.         tempString[++stringCount] = 0;
  96.         
  97.     return(stringCount);
  98.  
  99. }
  100.  
  101. /* now the methods */
  102.  
  103. /* ................................................................... */
  104. void CmyDataFile::ImyDataFile(void)
  105. /*
  106.     Name: Wade Maxfield
  107.     Date: November 25, 1989
  108.     Notes:
  109.     initalize this object
  110.     Modification History:
  111. */
  112. {
  113.  
  114.     /* call the superclass function */
  115.     CDataFile::IDataFile();
  116. }
  117.  
  118. /* ................................................................... */
  119. OSErr CmyDataFile::ReadLine(Ptr buffer, long howMuch, long *bytesRead)
  120. /*
  121.     Name: Wade Maxfield
  122.     Date: November 25, 1989
  123.     Notes:
  124.     read a line from the file into the buffer, stopping at carriage return
  125.     return an ACK or a NAK
  126.     Modification History:
  127. */
  128. {
  129. long returnValue;
  130.  
  131.     returnValue = ReadaLine(refNum,buffer, howMuch);
  132.      
  133.     if (returnValue < 0 )
  134.         {
  135.         *bytesRead = 0;
  136.         return(NAK);
  137.         }
  138.     else
  139.         {
  140.         *bytesRead = returnValue;
  141.         return(ACK);
  142.         }
  143. }
  144.  
  145.  
  146.